<?php
/* going to removed all of the SELECT abilities from MySQLCRUD and place them into MySQLQuery */
class MySQLQuery extends Query
{
	public $resultsAsArray = false;
	public $limit;
	public $offset;
	
	function __construct($ORMObjectString)
	{
		parent::__construct($ORMObjectString);
	}
	
	public function __toString()
	{
		if(!$this->sql)
		{
			
			$target     = new $this->domain();
			$this->sql = 'SELECT ';
			//$this->sql .= MySQLCRUD::buildSelectQuery($target, $this->greedy);
			
			$this->sql .= MySQLCRUD::appendColumns($target, $this->greedy);
			$this->sql .= MySQLCRUD::appendTables($this->tables);
			$this->sql .= MySQLCRUD::appendLeftJoins($target, $this->greedy);
			$target->__destruct();
				
			if($this->filters)
				$this->sql .= MySQLCRUD::appendFilters($this->filters);
			
			if($this->sorts)
				$this->sql .= MySQLCRUD::appendSorting($this->sorts);
				
			
			if($this->limit)
			{
				$this->sql .= MySQLCRUD::appendLimit($this->limit);
			
				if($this->offset)
					$this->sql .= MySQLCRUD::appendOffset($this->offset);
			}
		}
		
		return $this->sql;
	}
	
	public function count()
	{
		$sql  = MySQLCRUD::buildCountQuery($this->domain);
		$sql .= MySQLCRUD::appendTables($this->tables);
		
		if($this->filters)
			$sql .= MySQLCRUD::appendFilters($this->filters);
		
		$dbh  =& Database::connection();
		$result = $dbh->prepare($sql);
		$result->execute();
		Database::commit();
		$dbh = null;
		
		$row  = $result->fetch(PDO::FETCH_NUM);
		$result = null;
		return $row[0];
	}
}
?>